<?php
/**
 *计算时区间的差值 单位秒
 *@param $remote;
 *@param $origin;如果为空则使用当前时区
 *@return $offset;时区间的时差的时间戳
 */
function yf_get_timezone_offset($remote, $origin = '')
{
	if(empty($origin))
	{
		if(!is_string($origin = date_default_timezone_get()))
		{
			return false;
		}
	}
	$origin1 = new DateTimeZone($origin);
	$remote1 = new DateTimeZone($remote);
	$origin2 = new DateTime('now', $origin1);
	$remote2 = new DateTime('now', $remote1);
	$offset = $origin1->getOffset($origin2) - $remote1->getOffset($remote2);
	return $offset;
}

/**
 * 获取计算后的当前时间戳
 * @return $time;时间戳
 */
function yf_time()
{
	$offset = yf_get_timezone_offset('PRC');
	$time = time() - $offset;
	return $time;
}

/*
 * 
 */
function insert($info,$table)
{
    
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    
    foreach($info as $k=>$v)
    {
        if($v!='')
        {
            $field.="$k,";


            $value.="'$v',";
            	
        }
    }
    	
    	
    $field=substr($field,0,strlen($field)-1);
    $value=substr($value,0,strlen($value)-1);
    	
    $sql="INSERT INTO ".$ecs->table($table)." (".$field.") VALUES(".$value.")";
    
    
    if(!$res=$db->query($sql))
    {
        return false;
    }
    else
    {
        return $db->insert_id();
    }
}

function mkThumbnail($src, $width = null, $height = null, $filename = null) {
    if (!isset($width) && !isset($height))
        return false;
    if (isset($width) && $width <= 0)
        return false;
    if (isset($height) && $height <= 0)
        return false;
    
    $size = getimagesize($src);
    
    
    if (!$size)
        return false;

    list($src_w, $src_h, $src_type) = $size;
    $src_mime = $size['mime'];
    switch($src_type) {
        case 1 :
            $img_type = 'gif';
            break;
        case 2 :
            $img_type = 'jpeg';
            break;
        case 3 :
            $img_type = 'png';
            break;
        case 15 :
            $img_type = 'wbmp';
            break;
        default :
            return false;
    }

    if (!isset($width))
        $width = $src_w * ($height / $src_h);
    if (!isset($height))
        $height = $src_h * ($width / $src_w);

    $imagecreatefunc = 'imagecreatefrom' . $img_type;
    $src_img = $imagecreatefunc($src);
    $dest_img = imagecreatetruecolor($width, $height);
    imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);

    $imagefunc = 'image' . $img_type;
    if ($filename) {
        $imagefunc($dest_img, $filename);
    } else {
        header('Content-Type: ' . $src_mime);
        $imagefunc($dest_img);
    }
    imagedestroy($src_img);
    imagedestroy($dest_img);
    return true;
}

function subHeadimgurl($headimgurl){
	if($headimgurl){
		$headimgurl=substr($headimgurl,0,strlen($headimgurl)-1)."64";
	}
	return $headimgurl;
}
